<template>
  <div class="login-page">
    <div class="back">
      <!-- $router.back() 返回上一次访问路由，forward go -->
      <i class="iconfont icon-fanhui" @click="$router.back()"></i>
    </div>
    <h3 class="title">短信登录</h3>
    <!-- 表单 -->
    <van-form  ref="loginForm">
      <van-field placeholder="请输入手机号"  name="mobile" v-model="loginForm.mobile"  :rules="rules.mobile"></van-field>
      <van-field placeholder="请输入验证码" name="code" v-model="loginForm.code"   :rules="rules.code">
        <template #button>
          <span class="send" @click="send">  {{ second === 0 ? '发送验证码' : `${second}秒后发送` }}</span>
        </template>
      </van-field>
      <van-button  @click="onSubmit">登录</van-button>
    </van-form>

  </div>
</template>

<script>
// import { setToken } from '@/utils/storage'
import { getUser, getVerificationCode } from '@/api/user'
export default {
  name: 'LoginIndex',
  data () {
    return {
      // 登录表单数据
      loginForm: {
        mobile: '18612345678',
        code: '246810'
      },
      // 登录表单校验规则
      rules: {
        mobile: [{ required: true, message: '请填写手机号' }, { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不对' }],
        code: [{ required: true, message: '请填写验证码' }, { pattern: /^\d{6}$/, message: '验证码是6个数字' }]
      },
      // 倒计时秒数
      second: 0

    }
  },
  methods: {
    // 点击登录按钮时
    async onSubmit () {
      // 表单进行校验
      await this.$refs.loginForm.validate()
      // 调用登录接口
      const [err, res] = await getUser(this.loginForm)
      console.log(err, res)
      // 如果失败时提示登录失败，中止代码
      if (err) return this.$toast.fail('登录失败')
      // 存token到vuex
      this.$store.commit('setToken', res.data.token)
      // 跳转原页面或首页
      this.$router.push(this.$route.query.returnUrl || '/')
    },
    // 点击发送验证码时
    async send () {
      // 已发送，不做事
      if (this.second > 0) return
      // 校验手机号
      await this.$refs.loginForm.validate('mobile')
      // 调用发送验证码接口
      const [err] = await getVerificationCode(this.loginForm.mobile)
      // 如果失败，返回失败提示并终止代码
      if (err) return this.$toast.fail('发送失败')
      // 成功时开始倒计时
      this.second = 60
      // 定时器已有时先清除已有
      if (this.timer) clearInterval(this.timer)
      // 存储定时器ID，这个数据不参与视图渲染，所以不需要事先在data中定义
      this.timer = setInterval(() => {
        this.second-- // 每秒减一
        if (this.second <= 0) clearInterval(this.timer) // 倒计时为0时清除定时器
      }, 1000)
    }

  },
  // 销毁前清除所有定时器
  beforeDestroy () {
    if (this.timer) clearInterval(this.timer)
  }

}
</script>

<style scoped lang="less">
.login-page {
  padding: 0 32px;
  .back {
    height: 60px;
    display: flex;
    align-items: center;
    .icon-esay-close {
      font-size: 20px;
      color: #ccc;
      position: relative;
      left: -15px;
    }
  }
  .title {
    font-size: 22px;
    line-height: 1;
    padding: 30px 0;
  }
  .van-cell {
    padding: 20px 0;
    &::after {
      left: 0;
      right: 0;
    }
  }
  .van-button {
    width: 100%;
    margin-top: 40px;
    height: 50px;
    color: #fff;
    font-size: 16px;
    border: none;
    background: linear-gradient(to right, #FF9999, #FFA179);
  }
  .send {
    font-size: 12px;
    color: #A5A6AB;
  }
}
</style>
